#!/usr/bin/env python3
# -*-coding:utf-8-*-
# author:link
import requests
import json

class RunMethod:
    # post请求
    def post_m(self, url, data, header=None):
        res = None
        if header != None:
            res = requests.post(url=url, json=data, headers=header, verify=False)
        else:
            res = requests.post(url=url, json=data, verify=False)
        return res.json()
    def post_f(self, url, data, file, header=None):
        res = None
        files = [('file', open(file,'rb'))]
        if header != None:
            res = requests.post(url=url, data=data, files=files,headers=header, verify=False)
        else:
            res = requests.post(url=url, data=data, files=files, verify=False)
        return res.json()
    # get请求
    def get_m(self, url, data=None, header=None):
        res = None
        if header != None:
            res = requests.get(url=url, params=data, headers=header, verify=False)
        else:
            res = requests.get(url=url, params=data, verify=False)
        return res.json()
    def get_url(self, url, data=None, header=None):
        res = None
        if header != None:
            res = requests.get(url=url+data, headers=header, verify=False)
        else:
            res = requests.get(url=url+data, verify=False)
        return res.json()
    def get_url_m(self, url, url_data=None, data=None, header=None):
        res = None
        if header != None:
            res = requests.get(url=url+url_data, params=data, headers=header, verify=False)
        else:
            res = requests.get(url=url+url_data, params=data, verify=False)
        return res.json()
    # delete请求
    def delete_m(self, url, data=None, header=None):
        res = None
        if header != None:
            res = requests.delete(url=url, params=data, headers=header, verify=False)
        else:
            res = requests.delete(url=url, params=data, verify=False)
        return res.json()
    def delete_url(self, url, data=None, header=None):
        res = None
        if header != None:
            res = requests.delete(url=url+data, headers=header, verify=False)
        else:
            res = requests.delete(url=url+data, verify=False)
        return res.json()

    def delete_url_m(self, url, url_data=None, data=None, header=None):
        res = None
        if header != None:
            res = requests.delete(url=url+url_data, params=data, headers=header, verify=False)
        else:
            res = requests.delete(url=url+url_data, params=data, verify=False)
        return res.json()

    def put(self, url, data=None, header=None):
        res = None
        if header != None:
            res = requests.put(url=url, json=data, headers=header, verify=False)
        else:
            res = requests.delete(url=url, json=data, verify=False)
        return res.json()

    def run_main(self, method, url, data=None, url_data=None, file=None, header=None):
        res = None
        #post请求+json参数
        if method == 'post_m':
            res = self.post_m(url, data, header)
        # post请求+json参数+file文件
        elif method == 'post_f':
            res = self.post_f(url, data, file, header)
        #get请求带参数
        elif method == 'get_m':
            res = self.get_m(url, data, header)
        # get请求参数在url里
        elif method == 'get_url':
            res = self.get_url(url, data, header)
        # get请求参数在url里+参数在param里
        elif method =='get_url_m':
            res =self.get_url_m(url, url_data, data, header)
        #delete请求
        elif method == 'delete':
            res = self.delete_m(url, data, header)
        elif method == 'delete_url':
            res = self.delete_url(url, data, header)
        elif method == 'put':
            res = self.put(url, data, header)
        else:
            print("请求方式错误")
        return json.dumps(res, indent=2, sort_keys=True, ensure_ascii=False)

